package com.estronger.shopmanage.adapter.tasking;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.B16PlanMeasure;
import com.estronger.shopmanage.bean.conditionitem;
import com.estronger.shopmanage.bean.measureitem;
import com.estronger.shopmanage.manager.DataManager;
import com.estronger.shopmanage.view.CustomListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Soonfor on 2017/5/11.
 */

public class MsCoditionAdapter extends BaseAdapter {

    private Context mContext;
    private List<B16PlanMeasure.MeasureGoodsCondition> mList;
    private String[] pos = {"①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩","⑪","⑫","⑬","⑭","⑮","⑯","⑰","⑱","⑲","⑳"};
    private boolean isDetail;
    private String mesaureitemID;
    CustomListView conListview;

    public MsCoditionAdapter(Context context, String itemID, List<B16PlanMeasure.MeasureGoodsCondition> mList, boolean isDetail, CustomListView conList) {
        mContext = context;
        mesaureitemID = itemID;
        this.mList = mList;
        this.isDetail = isDetail;
        this.conListview = conList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final B16PlanMeasure.MeasureGoodsCondition mConditon = mList.get(position);
        final ViewHolder vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_pg_mscodition_item, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (pos == null || pos.length == 0) {
            pos = new String[]{"①","②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩","⑪","⑫","⑬","⑭","⑮","⑯","⑰","⑱","⑲","⑳"};
        }
        try{
        vh.tvfSerNumber.setText(pos[position]);
        }catch (Exception e){
            vh.tvfSerNumber.setText("...");
        }

        vh.tvfCondtion.setText(mConditon.content);
        if (isDetail) {
            vh.checkBox.setVisibility(View.GONE);
        } else {
            vh.checkBox.setVisibility(View.VISIBLE);
            if(mConditon.isChecked){
                vh.checkBox.setChecked(true);
            }else{
                vh.checkBox.setChecked(false);
            }
            vh.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mConditon.isChecked()) {
                        vh.checkBox.setChecked(false);
                        DataManager.condMap.get(mesaureitemID).get(position).setChecked(false);
                        mConditon.setChecked(false);
                    } else {
                        vh.checkBox.setChecked(true);
                        DataManager.condMap.get(mesaureitemID).get(position).setChecked(true);
                        mConditon.setChecked(true);
                    }
                    DataManager.msiteMap.put(mesaureitemID,
                            new measureitem(mesaureitemID,putCondition(DataManager.condMap.get(mesaureitemID))));
                }
            });
            vh.rlfItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mConditon.isChecked()) {
                        vh.checkBox.setChecked(false);
                        DataManager.condMap.get(mesaureitemID).get(position).setChecked(false);
                        mConditon.setChecked(false);
                    } else {
                        vh.checkBox.setChecked(true);
                        DataManager.condMap.get(mesaureitemID).get(position).setChecked(true);
                        mConditon.setChecked(true);
                    }
                    DataManager.msiteMap.put(mesaureitemID,
                            new measureitem(mesaureitemID,putCondition(DataManager.condMap.get(mesaureitemID))));
                }
            });
        }
        return convertView;
    }

    public ArrayList<conditionitem> putCondition(List<B16PlanMeasure.MeasureGoodsCondition> yConditons){
        ArrayList<conditionitem> newConditons = new ArrayList<>();
        for(int i = 0; i<yConditons.size(); i++ ){
            B16PlanMeasure.MeasureGoodsCondition ycond = yConditons.get(i);
            String ifcheck = "0";
            if(ycond.isChecked){ifcheck = "1";}else{ifcheck = "0";}
            conditionitem cond = new conditionitem();
            cond.setItemid(ycond.itemid);
            cond.setIfcheck(ifcheck);
            newConditons.add(cond);
        }
        return newConditons;
    }

    class ViewHolder {
        RelativeLayout rlfItem;
        TextView tvfSerNumber, tvfCondtion;
        CheckBox checkBox;

        public ViewHolder(View view) {
            rlfItem = (RelativeLayout) view.findViewById(R.id.rlfConditionItem);
            tvfSerNumber = (TextView) view.findViewById(R.id.tvfSerialNumber);
            tvfCondtion = (TextView) view.findViewById(R.id.tvfCodtion);
            checkBox = (CheckBox) view.findViewById(R.id.cbSelector);
        }
    }
}
